package xhwl.insurance.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.provider.Contacts;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;


import java.util.ArrayList;

import xhwl.android.common.log.XHLog;
import xhwl.android.common.util.SPUtil;
import xhwl.android.common.util.ToastUtil;
import xhwl.insurance.R;
import xhwl.insurance.customView.CircleImageView;
import xhwl.insurance.http.bean.resp.CustomerServiceLeftResp;
import xhwl.insurance.ui.SmartServiceActivity;
import xhwl.insurance.util.ImgTools;


/**
 * Created by fuyi on 2017/10/8.
 */

public class SmartServiceAdapter extends RecyclerView.Adapter {
    public Context context;
    public ArrayList<CustomerServiceLeftResp> mlist;
    public Handler handler;


    public SmartServiceAdapter(Context context, ArrayList<CustomerServiceLeftResp> mlist, Handler handler) {
        this.context = context;
        this.mlist = mlist;
        this.handler = handler;

    }

    public static final int FROM_USER_MSG = 0;//接收消息类型
    public static final int TO_USER_MSG = 1;//发送消息类型

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case FROM_USER_MSG://接收
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_customerservice, parent, false);
                holder = new DoctorViewHolder(view);

                break;
            case TO_USER_MSG://发送
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_customerservice1, parent, false);
                holder = new PatientViewHolder(view);

                break;
        }
        return holder;
    }

    public int getItemViewType(int position) {
        return mlist.get(position).getIS_LEFT();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        CustomerServiceLeftResp bean = mlist.get(position);
        int itemViewType = getItemViewType(position);
        switch (itemViewType) {
            case FROM_USER_MSG:
                String answer_content = bean.getANSWER_CONTENT();
                fromMsgUserLayout((DoctorViewHolder) holder, bean,answer_content);
                break;
            case TO_USER_MSG://发送
                String answer_content2 = bean.getANSWER_CONTENT();
                String MEMBER_PIC = SPUtil.getPersonInfoSp().getString(SPUtil.SPPersonInfo.MEMBER_PIC, "");
                toMsgUserLayout((PatientViewHolder) holder, MEMBER_PIC,answer_content2);
                break;
        }

    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }

    private void fromMsgUserLayout(final DoctorViewHolder holder, final CustomerServiceLeftResp bean,String answer_content) {

        if (bean.getANSWER_CONTENT().contains("Hello")) {
            String[] text = bean.getANSWER_CONTENT().split("~");
            holder.content.setText(text[0] + "~\n");
            final String[] questions = text[1].split("\n\n");
            for (int i = 0; i < questions.length; i++) {
                final int index = i;
                final SpannableString ss = new SpannableString(questions[i] + "\n\n");
                ss.setSpan(new ClickableSpan() {
                    @Override
                    public void updateDrawState(TextPaint ds) {
                        // TODO Auto-generated method stub
                        super.updateDrawState(ds);
                        ds.setColor(context.getResources().getColor(R.color.logincolor));
                        ds.setUnderlineText(false);
                    }

                    @Override
                    public void onClick(View widget) {
                        // TODO Auto-generated method stub
                        Message message = new Message();
                        message.obj = questions[index];
                        handler.sendMessage(message);
                    }
                }, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                holder.content.append(ss);

            }
            holder.content.setMovementMethod(LinkMovementMethod.getInstance());

        } else {
            xhwl.insurance.util.XHLog.e("1:" + answer_content);
            holder.content.setText(Html.fromHtml(answer_content));

        }
    }

    private void toMsgUserLayout(final PatientViewHolder holder2, String pic,String answer_content2) {
        holder2.content2.setText(Html.fromHtml(answer_content2));
        Glide.with(context).load(pic).error(R.drawable.userhead).into(holder2.img_doctorheader);
    }

    class DoctorViewHolder extends RecyclerView.ViewHolder {
        private TextView content;


        public DoctorViewHolder(View itemView) {
            super(itemView);
            content = (TextView) itemView.findViewById(R.id.chat);

        }
    }

    class PatientViewHolder extends RecyclerView.ViewHolder {
        private TextView content2;
        private CircleImageView img_doctorheader;

        public PatientViewHolder(View itemView) {
            super(itemView);
            img_doctorheader = (CircleImageView) itemView.findViewById(R.id.img_doctorheader);
            content2 = (TextView) itemView.findViewById(R.id.chat_doctor);
        }
    }
}
